package base.action.lib;

import java.io.File;
import java.io.IOException;
import java.util.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends BaseAction {
	private static final long serialVersionUID = -4848248679889814408L;



	private List<File> Filedata;
	private List<String> FiledataFileName;
	private List<String> FiledataContentType;
	
	public List<File> getFiledata() {
		return Filedata;
	}
	
	
	public void setFiledata(List<File> filedata) {
		Filedata = filedata;
	}
	
	
	public List<String> getFiledataFileName() {
		return FiledataFileName;
	}
	
	
	public void setFiledataFileName(List<String> filedataFileName) {
		FiledataFileName = filedataFileName;
	}
	
	
	public List<String> getFiledataContentType() {
		return FiledataContentType;
	}
	
	
	public void setFiledataContentType(List<String> filedataContentType) {
		FiledataContentType = filedataContentType;
	}



	public String input() {
		    HttpServletResponse response = ServletActionContext.getResponse();
			ActionContext ac=ActionContext.getContext();
			ServletContext sc = (ServletContext)ac.get(ServletActionContext.SERVLET_CONTEXT);
			String savePath = sc.getRealPath("/");
			savePath = savePath + "upload\\test\\";
			File f1 = new File(savePath);
			if (!f1.exists()) {
				f1.mkdirs();
			}
			int size = Filedata.size();
			if (size == 0){
				return ERROR;
			}
			String extName = null;
	        String url = null;
			for (int i = 0; i < size; i++) {
				extName = FiledataFileName.get(i);
			    File file = new File(savePath + extName);
				try {
					FileUtils.copyFile(Filedata.get(i), file);
                    url="upload/test/" + extName;
					this.setAjaxMessage(url);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			response.setCharacterEncoding("utf-8");
			return "ajax";
	}

}